package com.ccjq.shosestore.product.util;

public class PageBean {

    private static int totalRows; // 总行数
    private static int pageSize; // 每页显示几行
    private static int totalPages; // 总页数
    private static int currentPage;// 当前页数
    private static int startRow; // 当前页起始行数

    public PageBean(int _totalRows) { // 构造方法中求总页数
        this.totalRows = _totalRows;
        this.pageSize = 4;// 手动设置每页显示数，也可以从前台获取 让用户指定每页显示多少条数据
        this.currentPage = 1;// 当前行数为1
        this.startRow = 1; // 起始行数为1
        // 判断总行数/每页行数是不是整除，如果不是则需要+1页
        this.totalPages = (totalRows % pageSize == 0) ? (totalRows / pageSize) : (totalRows / pageSize + 1);
        // System.out.println("TOP"+totalPages);

    }

    public static int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    public static int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public static int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public static int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    // 首页
    public void first() {
        currentPage = 1;
        startRow = 1;
    }

    // 上一页
    public void previous() {
        if (currentPage == 1) { // 当前为第一页时不调用
            return;
        }
        startRow = (--currentPage - 1) * pageSize + 1; // 跳转到上一页后的起始行

    }

    // 下一页
    public void next() {

        if (currentPage < totalPages) {
            currentPage++;
        }

        startRow = (currentPage - 1) * pageSize + 1;

    }

    // 尾页
    public void last() {
        currentPage = totalPages; // 跳转到最大页数
        startRow = (currentPage - 1) * pageSize + 1;
    }

    // 跳转
    public void refresh(int _currentPage) {
        currentPage = _currentPage;
        if (currentPage > totalPages) { // 判断跳转目标是否超过最大页数
            this.last();
        } else if (currentPage < 1) {
            this.first();
        }
        startRow = (currentPage - 1) * pageSize + 1;

    }

    // 跳转
    public void refresh(int _currentPage, String pageMethod) {
        currentPage = _currentPage;
//        if (currentPage > totalPages) { // 判断跳转目标是否超过最大页数
//            this.last();
//        } else if (currentPage < 1) {
//            this.first();
//        }else{
        if ("next".equals(pageMethod)) {
            this.next();
        }
        if ("previous".equals(pageMethod)) {
            this.previous();
        }
        if ("first".equals(pageMethod)) {
            this.first();
        }
        if ("last".equals(pageMethod)) {
            this.last();
        }
//        }
        startRow = (currentPage - 1) * pageSize + 1;

    }
}
